package com.budwk.app.waterquality.controllers.admin;

import com.budwk.app.waterquality.config.AppException;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.app.waterquality.services.FileInfoService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.nutz.mvc.impl.AdaptorErrorContext;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/waterquality/admin/file/info")
@ApiDefinition(tag = "文件存储信息表")
@SLog(tag = "文件存储信息表")
@Slf4j
public class FileInfoController {
    @Inject
    private FileInfoService fileInfoService;

    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:fileUpload"})
    @POST
    @At
    @Ok("json")
    @ApiOperation(name = "上传文件")
    @ApiFormParams(value = {@ApiFormParam(name = "file", example = "", description = "文件表单对象名"),},mediaType = "multipart/form-data")
//    @SaCheckLogin
    public Result<?> uploadFile(@Param("file") TempFile file, AdaptorErrorContext err) {
        if (err != null && err.getAdaptorErr() != null) {
            throw new AppException(log.isDebugEnabled() ? err.getAdaptorErr().getMessage() : "文件服务异常");
        }
        if (file == null) {
            throw new AppException("文件不可为空");
        }
        return fileInfoService.uploadFile(file);
    }

}
